package com.itcodai.controller.course11;

import com.itcodai.entity.User;
import com.itcodai.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author c-fanqj
 * @date 2023/4/4
 * @apiNote
 */
@RestController
@RequestMapping("/user")
@Api(value = "事务基本操作")
public class TestTransactionalController {

    private final Logger logger =
            LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserService userService;

    /**
     * 正常测试
     * @param user
     * @return
     */
    @PostMapping("/adduser")
    @ApiOperation(value = "添加用户信息")
    public String addUser(
            @RequestBody @ApiParam(value = "用户信息") User user
    ) throws Exception {
        if (null != user) {
            logger.info("传入的参数user：{}", user);
            userService.insertUser(user);
            return "success";
        } else {
            return "false";
        }
    }

    /**
     * 测试异常并没有被捕获到
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/adduser2")
    @ApiOperation(value = "异常没有被捕获到")
    public String addUser2(
            @RequestBody @ApiParam(value = "用户信息") User user
    ) throws Exception {
        if (null != user) {
            userService.insertUser2(user);
            return "success";
        } else {
            return "false";
        }
    }

    /**
     * 测试异常被吃掉
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/adduser3")
    @ApiOperation(value = "测试异常被吃掉")
    public String addUser3(
            @RequestBody @ApiParam(value = "用户信息") User user
    ) {
        if (null != user) {
            userService.insertUser3(user);
            return "success";
        } else {
            return "false";
        }
    }

}
